package com.azlogica.entity;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.BatteryManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

public class Frame {

	Location location;
	public String frame;
	Context c;
	TelephonyManager mngr;
	GsmCellLocation cl;
	// ---------Frame attributes-----------//
	int id;
	String header;
	String report;
	String sack;
	String messType;
	String imei;
	String status;
	String number;
	String accuracy;
	String speed;
	String azimuth;
	String altitude;
	String longitude;
	String latitude;
	String gpsUTC;
	String mcc;
	String mnc;
	String lac;
	String cllId;
	String battery;
	String sendTime;
	String serial;
	int send;

	public Frame(Location location, Context context) {
		this.location = location;
		c = context;
		mngr = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		cl = (GsmCellLocation) mngr.getCellLocation();
		IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent batteryStatus = c.registerReceiver(null, ifilter);
		int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		Date date = new Date(location.getTime());
		Calendar cal = Calendar.getInstance();
		String networkOperator = mngr.getNetworkOperator();

		// ------------------------Some parameters
		// before-----------------------//

		this.header = "0005";
		this.report = "001F";
		this.number = "01";
		this.sack = "00";
		this.messType="04";
		String formattedUTC = format.format(date);
		this.mcc = networkOperator.substring(0, 3);
		this.mnc = networkOperator.substring(3);
		String formattedActualTime = format.format(cal.getTime());

		this.imei = Long.toHexString(Long.parseLong(mngr.getDeviceId()));
		this.accuracy = Integer.toHexString(Integer.parseInt(String.valueOf(
				location.getAccuracy()).replace(".", "")));
		this.speed = Integer.toHexString(Integer.parseInt(String.valueOf(
				location.getSpeed()).replace(".", "")));
		this.azimuth = Integer.toHexString(Integer.parseInt(String.valueOf(
				location.getBearing()).replace(".", "")));
		this.altitude = Integer.toHexString(Integer.parseInt(String.valueOf(
				location.getAltitude()).replace(".", "")));
		this.longitude = Integer.toHexString(Integer.parseInt(String.valueOf(
				location.getLongitude()).replace(".", "")));
		this.latitude = Integer.toHexString(Integer.parseInt(String.valueOf(
				location.getLatitude()).replace(".", "")));
		this.gpsUTC = Long.toHexString(Long.parseLong(formattedUTC));
		this.mcc = "0" + mcc;
		this.mnc = "0" + mnc;
		this.lac = "0" + cl.getLac();
		this.cllId = "" + cl.getCid();
		this.battery = Integer.toHexString(level);
		this.sendTime = Long.toHexString(Long.parseLong(formattedActualTime));

	}

	public Frame(int id, String header, String report, String sack,
			String messType, String imei, String status, String number,
			String accuracy, String speed, String azimuth, String altitude,
			String longitude, String latitude, String gpsUTC, String mcc,
			String mnc, String lac, String cllId, String battery,
			String sendTime, String serial, int send) {
		super();
		this.id = id;
		this.header = header;
		this.report = report;
		this.sack = sack;
		this.messType = messType;
		this.imei = imei;
		this.status = status;
		this.number = number;
		this.accuracy = accuracy;
		this.speed = speed;
		this.azimuth = azimuth;
		this.altitude = altitude;
		this.longitude = longitude;
		this.latitude = latitude;
		this.gpsUTC = gpsUTC;
		this.mcc = mcc;
		this.mnc = mnc;
		this.lac = lac;
		this.cllId = cllId;
		this.battery = battery;
		this.sendTime = sendTime;
		this.serial = serial;
		this.send = send;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public TelephonyManager getMngr() {
		return mngr;
	}

	public void setMngr(TelephonyManager mngr) {
		this.mngr = mngr;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getHeader() {
		return header;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	public String getReport() {
		return report;
	}

	public void setReport(String report) {
		this.report = report;
	}

	public String getSack() {
		return sack;
	}

	public void setSack(String sack) {
		this.sack = sack;
	}

	public String getMessType() {
		return messType;
	}

	public void setMessType(String messType) {
		this.messType = messType;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(String accuracy) {
		this.accuracy = accuracy;
	}

	public String getSpeed() {
		return speed;
	}

	public void setSpeed(String speed) {
		this.speed = speed;
	}

	public String getAzimuth() {
		return azimuth;
	}

	public void setAzimuth(String azimuth) {
		this.azimuth = azimuth;
	}

	public String getAltitude() {
		return altitude;
	}

	public void setAltitude(String altitude) {
		this.altitude = altitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getGpsUTC() {
		return gpsUTC;
	}

	public void setGpsUTC(String gpsUTC) {
		this.gpsUTC = gpsUTC;
	}

	public String getMcc() {
		return mcc;
	}

	public void setMcc(String mcc) {
		this.mcc = mcc;
	}

	public String getMnc() {
		return mnc;
	}

	public void setMnc(String mnc) {
		this.mnc = mnc;
	}

	public String getLac() {
		return lac;
	}

	public void setLac(String lac) {
		this.lac = lac;
	}

	public String getCllId() {
		return cllId;
	}

	public void setCllId(String cllId) {
		this.cllId = cllId;
	}

	public String getBattery() {
		return battery;
	}

	public void setBattery(String battery) {
		this.battery = battery;
	}

	public String getSendTime() {
		return sendTime;
	}

	public void setSendTime(String sendTime) {
		this.sendTime = sendTime;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public int getSend() {
		return send;
	}

	public void setSend(int send) {
		this.send = send;
	}

	// // public String getFrame() {
	// IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
	// Intent batteryStatus = c.registerReceiver(null, ifilter);
	// int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
	// SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
	// Date date = new Date(location.getTime());
	// Calendar cal = Calendar.getInstance();
	// String networkOperator = mngr.getNetworkOperator();
	//
	// // ------------------------Some parameters
	// // before-----------------------//
	//
	// String header = "0005";
	// String number = "01";
	// String sack = "00";
	// String formattedUTC = format.format(date);
	// String mcc = networkOperator.substring(0, 3);
	// String mnc = networkOperator.substring(3);
	// String formattedActualTime = format.format(cal.getTime());
	//
	// // --------------------Built of the
	// // frame-------------------------------//
	// frame = "";
	// frame += header;
	// frame += sack;
	// frame += "" + Long.toHexString(Long.parseLong(mngr.getDeviceId()));
	// frame += "" + number;
	// frame += ""
	// + Integer.toHexString(Integer.parseInt(String.valueOf(
	// location.getAccuracy()).replace(".", "")));
	// frame += ""
	// + Integer.toHexString(Integer.parseInt(String.valueOf(
	// location.getSpeed()).replace(".", "")));
	// frame += ""
	// + Integer.toHexString(Integer.parseInt(String.valueOf(
	// location.getBearing()).replace(".", "")));
	// frame += ""
	// + Integer.toHexString(Integer.parseInt(String.valueOf(
	// location.getAltitude()).replace(".", "")));
	// frame += ""
	// + Integer.toHexString(Integer.parseInt(String.valueOf(
	// location.getLongitude()).replace(".", "")));
	// frame += ""
	// + Integer.toHexString(Integer.parseInt(String.valueOf(
	// location.getLatitude()).replace(".", "")));
	// frame += "" + Long.toHexString(Long.parseLong(formattedUTC));
	// frame += "0" + mcc;
	// frame += "0" + mnc;
	// frame += "0" + cl.getLac();
	// frame += "" + cl.getCid();
	// frame += "" + Integer.toHexString(level);
	// frame += "" + Long.toHexString(Long.parseLong(formattedActualTime));
	//
	// separateFrame();
	// removeLeadingZeros();
	// return frame;
	// }
	//
	// void separateFrame() {
	// int interval = 2;
	// char separator = ' ';
	//
	// StringBuilder sb = new StringBuilder(frame);
	//
	// for (int i = 0; i < frame.length() / interval; i++) {
	// sb.insert(((i + 1) * interval) + i, separator);
	// }
	//
	// frame = sb.toString();
	// }
	//
	// void removeLeadingZeros() {
	// int interval = 2;
	// String temp = frame;
	// frame = "";
	// for (int i = 0; i < temp.length() - interval; i = i + 3) {
	// frame += " "
	// + temp.substring(i, i + 2).replaceFirst("^0+(?!$)", "");
	// }
	// frame = frame.substring(1, frame.length());
	// }

}